<?php

namespace app\shop\controller\sys_admin;

use app\AdminController;
use app\member\model\UsersModel;
use app\shop\model\MeetingCommissionLogModel;
use app\shop\model\MeetingModel;
use app\shop\model\SignInModel;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Encoding\Encoding;

class Meeting extends AdminController
{
    public $Model =null;

    public function initialize()
    {
        $this->Model =new MeetingModel();
        return parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function getList($runData = false)
    {
        $this->sqlOrder = "id DESC";
        $where =$this->Model->alias('m')->join('users u','u.user_id = m.user_id', 'left')->field('m.*,u.mobile,u.nick_name');
        $data = $this->getPageList($this->Model,$where);
        $arr =[0=>'未开始',1=>'进行中',2=>'已结束'];
        foreach ($data['list'] as &$item){
            $item['start_time'] = date('Y-m-d H:i',$item['start_time']);
            $item['end_time'] = date('Y-m-d H:i',$item['end_time']);
            $item['status_text'] = $arr[$item['status']];
        }
        $this->assign("data", $data);
        if (empty($runData)) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            $this->success('', '', $data);
        }
        return true;
    }

    public function getList2($runData = false)
    {
        $id =input('id');
        $this->sqlOrder = "id DESC";
        $model =new SignInModel();
        $where =$model->alias('s')->join('users u','u.user_id = s.user_id', 'left')
            ->join('meeting m','m.id = s.meeting_id')
            ->field('s.*,u.mobile,u.nick_name,m.meeting_name')
        ->where('s.meeting_id',$id);
        $data = $this->getPageList($model,$where);
        $this->assign("data", $data);
        if (empty($runData)) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            $this->success('', '', $data);
        }
        return true;
    }
    public function index()
    {
        $this->assign('title', '会议列表');
        $this->getList(true);
        return $this->fetch('sys_admin/meeting/index');
    }

    public function signin()
    {
        $this->assign('title', '签到记录');
        $this->getList2(true);
        return $this->fetch('sys_admin/meeting/signin');
    }

    public function qrcode()
    {
        $id =input('id');
        $url =$this->request->domain();
        $code =MeetingModel::where('id',$id)->value('sign_code');
        if(empty($code)){
            $this->error('会议不存在');
        }
        $url =$url.'/pages/member/meeting/sign-in?code='.$code;
        $this->assign('text',$url);
        return $this->fetch();
    }

    public function count(){
        $this->assign('title', '签到记录');
        $this->getList3(true);
        return $this->fetch('sys_admin/meeting/count');

    }

    public function getList3($runData = false)
    {
        $model =new MeetingCommissionLogModel();
        $id =input('id');
        $this->sqlOrder = "id DESC";
        $where =$model->alias('a')
            ->join('shop_goods g','a.goods_id=g.goods_id')
            ->join('users u','a.from_uid=u.user_id')
            ->join('shop_order_info o','o.order_id=a.order_id')
            ->join('meeting m','m.id=a.meeting_id')
            ->field('a.id,a.goods_id,a.status,a.total_price,m.meeting_name,g.goods_name,a.commission,a.commission_scale,u.nick_name,u.headimgurl,a.add_time,a.from_uid,a.user_id,o.order_sn')
            ->where('a.meeting_id',$id);
        $data = $this->getPageList($model,$where);
        $data['total_commission'] = $model->where('meeting_id',$id)->sum('commission');
        $data['total_price'] = $model->where('meeting_id',$id)->sum('total_price');
        $this->assign("data", $data);
        if (empty($runData)) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            $this->success('', '', $data);
        }
        return true;
    }

    public function info()
    {
        $userList =UsersModel::where(['is_dz'=>1])->field('user_id as value,nick_name as label')->select()->toArray();
        $goodsList =\app\shop\model\GoodsModel::where(['isputaway'=>1])
            ->field('goods_id,goods_name')->select();
        $this->assign("goodsList", $goodsList);
        $this->assign("goodsListJson", json_encode($goodsList));
        $this->assign("userList", $userList);
        return parent::info(); // TODO: Change the autogenerated stub
    }

    public function del()
    {
        $id = input('id', 0, 'intval');
        if ($id < 1)  $this->error('传递参数失败！');
        $data = $this->Model->where('id', $id)->find();
        $res = $data->delete();
        if ($res < 1)  $this->error();
        SignInModel::where('meeting_id',$id)->delete();
        $this->success('删除成功.');
    }

    public function asInfo($row)
    {
        return $row;
    }
    public function beforeAdd($data)
    {
        if ($data['id'] < 1) {
            $data['add_time'] = time();
        }
        if($data['goods_ids']){
            $arr =explode(",",$data['goods_ids']);
            $arr =array_unique($arr);
            $data['goods_ids'] =implode(',',$arr);
        }
        if($data['info']){
            $arrayData = json_decode($data['info'], true);
            $uniqueArray = array_reduce($arrayData, function ($carry, $item) {
                $goodsId = $item['goods_id'];
                if (!isset($carry[$goodsId])) {
                    $carry[$goodsId] = $item;
                }
                return $carry;
            }, []);
            $data['info'] = json_encode(array_values($uniqueArray));
        }
        $data['sign_code'] =random_str(8);
        if (empty($data['meeting_name'])) return $this->error('请填写会议名称');
        if (empty($data['user_id'])) return $this->error('请选择导师');
        $data['start_time']=strtotime($data['start_time']);
        $data['end_time']=strtotime($data['end_time']);
        return $data;
    }

    /*------------------------------------------------------ */
    //-- 修改前调用
    /*------------------------------------------------------ */
    public function beforeEdit($data)
    {
        return $this->beforeAdd($data);
    }


}